package com.mucd.chat.service;

import cn.hutool.core.util.ObjectUtil;
import com.mucd.chat.mapper.MessageRecordMapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.MessageEntity;
import com.ruoyi.common.core.domain.MessageRecord;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.MessageListModel;
import com.ruoyi.common.core.domain.model.MessageRecordModel;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mucd
 */
@Service
@Slf4j
public class MessageRecordService {

    @Resource
    private MessageRecordMapper messageRecordMapper;

    /**
     * 保存消息记录
     *
     * @param message      消息内容
     * @param targetUserId 接收消息人id
     * @param myUserId     发送人id
     */
    @Async
    public void saveMessageRecord(String message, Long targetUserId, Long myUserId,Integer onlineType) {

        MessageRecord messageRecord = new MessageRecord();
        messageRecord.setMessage(message);
        messageRecord.setType(Constants.MESSAGE_TYPE_TEXT);
        messageRecord.setReceiveUserId(targetUserId);
        messageRecord.setSendUserId(myUserId);
        messageRecord.setOnlineType(onlineType);
        log.info("保存消息记录:{}", messageRecord);

        int row = messageRecordMapper.insert(messageRecord);
        log.info("保存消息记录rows :{}", row);
    }

    /**
     * 查询消息记录
     *
     * @param myUserId     发送人id
     * @param targetUserId 接收消息人id
     * @return 消息记录
     */
    public List<MessageEntity> selectListBySendUserIdAndReceiveUserId(Long myUserId, Long targetUserId) {
        MessageRecord mr = new MessageRecord();
        mr.setSendUserId(myUserId);
        mr.setReceiveUserId(targetUserId);
        //查两人聊天记录
        List<MessageRecordModel> messageRecords = messageRecordMapper.selectListBySendUserIdAndReceiveUserId(mr);

        //set到实体类
        List<MessageEntity> result = messageRecords.stream().map(item -> {
            MessageEntity me = new MessageEntity();
            me.setAvatar(item.getAvatar());
            me.setSendSign(item.getSendUserId().equals(myUserId) ? "my" : "target");
            me.setMessage(item.getMessage());
            return me;
        }).collect(Collectors.toList());

//        MessageEntity entity = new MessageEntity();
//        entity.setMessage(mr.getMessage());
//        entity.setAvatar();
//        entity.setSendSign(sendSign);


        return result;
    }


    /**
     * 查询指定好友的未读数量
     * @param myUserId 自己的好友id
     */
    public Integer selectNotReadNum(Long myUserId) {

        if (ObjectUtil.isEmpty(myUserId) || myUserId <= 0) {
            return 0;
        }

        int num = messageRecordMapper.selectNotReadNum(myUserId);
        log.info("查询未读数量:{}", num);

        return num;
    }
}
